#include "HttpRequest.h"
#include "HttpResponse.h"
#include "Log.h"
#include "Util.h"
#include <echo.hpp>
#include <HttpServer.h>
#include <sstream>

using namespace muduo;
using namespace std;

#define WWWROOT "/home/lyt/linux-study/HttpServer/src/test/wwwroot/"

string ResStr(const HttpRequest& req){
    stringstream ss;
    ss << req._method << " " << req._path << " " << req._version << "\r\n";
    for(auto s:req._params){
        ss<<s.first << ": " <<s.second<<"\r\n";
    }
    for(auto s:req._header){
        ss<<s.first << ": " <<s.second<<"\r\n";
    }
    ss<<"\r\n";
    ss<<req._body;
    return ss.str();
}

void Hello_(const HttpRequest& req,HttpResponse* res){
    res->SetContent(ResStr(req), "text/plain");
}
void POST_(const HttpRequest& req,HttpResponse* res){
    res->SetContent(ResStr(req), "text/plain");
}

void PUT_(const HttpRequest& req,HttpResponse* res){
    debuglog<<"in put";
    std::string pathname= WWWROOT + req._path;
    Util::WriteFile(pathname, req._body);
}

void DELETE_(const HttpRequest& req,HttpResponse* res){
    res->SetContent(ResStr(req), "text/plain");
}


int main(){
    HttpServer server(38080);
    server.SetThreatPoolNum(12);
    server.SetBaseDir(WWWROOT);
    server.Get("/hello", Hello_);
    server.Post("/login", POST_);
    server.Put("/1234.txt", PUT_);
    server.Delete("/1234.txt", DELETE_);
    server.Listen();
    return 0;
}